#include "stdafx.h"
#include "XLEngine.h"
#include "XLDownload.h"
#include "HedEngineManager.h"

namespace XLEngine
{
	BOOL InitDownloadEngine()
	{
#ifdef _WIN64
		return TRUE;
#else
		return XLInitDownloadEngine();
#endif // _WIN64
	}

	DWORD URLDownloadToFile( LPCTSTR pszFileName, LPCTSTR pszUrl, LPCTSTR pszRefUrl, LONG& lTaskId )
	{
#ifdef _WIN64
		if (CHedEngineManager::GetSingletonPtr()->DownloadToFile(pszFileName, pszUrl, lTaskId))
		{
			return XL_SUCCESS;
		}
		else
		{
			return XL_ERROR_FAIL;
		}
#else

#ifdef _UNICODE
		return XLURLDownloadToFile(pszFileName, pszUrl, pszRefUrl, lTaskId);
#else
		USES_CONVERSION;
		return XLURLDownloadToFile(A2CW(pszFileName), A2CW(pszUrl), A2CW(pszRefUrl), lTaskId);
#endif

#endif // _WIN64
	}

	DWORD QueryTaskInfo( LONG lTaskId, LONG* plStatus, ULONGLONG* pullFileSize, ULONGLONG* pullRecvSize )
	{
#ifdef _WIN64
		return CHedEngineManager::GetSingletonPtr()->QueryTaskInfo(lTaskId, plStatus, pullFileSize, pullRecvSize);
#else
		return XLQueryTaskInfo(lTaskId, plStatus, pullFileSize, pullRecvSize);
#endif // _WIN64
	}

	DWORD PauseTask( LONG lTaskId, LONG& lNewTaskId )
	{
#ifdef _WIN64
		DWORD dwResult = CHedEngineManager::GetSingletonPtr()->PauseDown(lTaskId);
		lNewTaskId = lTaskId;
		return dwResult;
#else
		return XLPauseTask(lTaskId, lNewTaskId);
#endif // _WIN64
	}

	DWORD ContinueTask( LONG lTaskId )
	{
#ifdef _WIN64
		return CHedEngineManager::GetSingletonPtr()->ContinueDown(lTaskId);
#else
		return XLContinueTask(lTaskId);
#endif // _WIN64
	}

	DWORD ContinueTaskFromTdFile( LPCTSTR pszTdFileFullPath, LONG& lTaskId )
	{
#ifdef _WIN64
		return XL_ERROR_FAIL;
#else

#ifdef _UNICODE
		return XLContinueTaskFromTdFile(pszTdFileFullPath, lTaskId);
#else
		USES_CONVERSION;
		return XLContinueTaskFromTdFile(A2CW(pszTdFileFullPath), lTaskId);
#endif

#endif // _WIN64
	}

	VOID StopTask( LONG lTaskId )
	{
#ifndef _WIN64
		XLStopTask(lTaskId);
#endif // _WIN64
		CHedEngineManager::GetSingletonPtr()->StopTaskById(lTaskId);
	}

	BOOL UninitDownloadEngine()
	{
#ifdef _WIN64
		CHedEngineManager::GetSingletonPtr()->ReleaseDownTaskInfo();
		return TRUE;
#else
		return XLUninitDownloadEngine();
#endif // _WIN64
	}

	DWORD GetErrorMsg( DWORD dwErrorId, LPTSTR pszBuffer, DWORD& dwSize )
	{
#ifdef _WIN64
		return XL_ERROR_FAIL;
#else

#ifdef _UNICODE
		return XLGetErrorMsg(dwErrorId, pszBuffer, dwSize);
#else
		USES_CONVERSION;
		LPWSTR lpszErrorMsg = A2W(pszBuffer);
		DWORD dwRet = XLGetErrorMsg(dwErrorId, lpszErrorMsg, dwSize);

		_tcscpy(pszBuffer, W2A(lpszErrorMsg));
		return dwRet;
#endif

#endif // _WIN64
	}
}
